<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部 -->
<head th:replace="admin/layout/include::head('页面管理','/assets/libs/codemirror/codemirror.css,/assets/libs/codemirror/darcula.css')"></head>
<style>
    #runjsParent #runjs{
        display: none;
        height: 100%;
    }
    #runjs .layui-row {
        height: 100%;
    }
    #runjs .layui-row>div{
        height: 97%;
    }
    #runjs .site-demo-btn1 {
        position: absolute;
        bottom: 15px;
        right: 20px;
        z-index: 10000;
    }
    #runjs .site-demo-btn2 {
        position: absolute;
        bottom: 15px;
        right: 90px;
        z-index: 10000;
    }
    #runjs .CodeMirror {
        width: 100%;
        height: 100%;
    }
    #runjsDemo {
        width: 90%;
        height: 90%;
        border: none;
        padding: 10px;
    }
</style>
<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                       <!-- <button id="codeEdit" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>代码编辑</button>-->
                    </div>
                    <div class="layui-inline" style="margin-left: 50px;" lay-tips="使用页面前面加 /page/" lay-direction="2">
                        <span class="icon-text" ><i class="layui-icon layui-icon-tips"></i></span>
                        注意事项
                    </div>
                </div>
            </div>

            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
<div id="runjsParent">

    <div id="runjs">
        <div class="layui-row">
            <div class="layui-col-xs6">
                <input id="codePageId" type="hidden"/>
                <textarea id="code"></textarea>
                <div class="site-demo-btn2">
                    <a type="button" class="layui-btn" id="code_save">保存</a>
                </div>
                <div class="site-demo-btn1">
                    <a type="button" class="layui-btn" id="LAY_demo_run">预览</a>
                </div>
            </div>
            <div class="layui-col-xs6">
                <iframe id="runjsDemo" src="/assets/libs/codemirror/demo.html">

                </iframe>
            </div>
        </div>
    </div>
</div>
</body>

<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-xs btnCopy" data-clipboard-text="/{{d.pageCode}}"><i class="layui-icon">&#xe6b2;</i>复制地址</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="codeEdit">代码编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="data-table-state" value="{{d.pageId}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>
<script type="text/html" id="prefixHtml">
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>在线调试</title>
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <link rel="stylesheet" href="/assets/libs/layui/css/layui.css" media="all"/>
        <link rel="stylesheet" href="http://portal.javabb.cn/static/assets/css/global.css" media="all"/>

    </head>
    <body style="margin-top: 0px;">
    <div class="layui-card">
        <div class="layui-card-body">
</script>

<!--类型-->
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<div th:include="admin/layout/include::codejs"></div>
<script src="https://cdn.bootcss.com/clipboard.js/2.0.4/clipboard.min.js"></script>
<script>
    layui.use(['form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var layer = layui.layer;
        var formUrl;  // 表单提交的action

        //渲染表格
        var tableIns = table.render({
            id: 'page-table-id',
            elem: '#data-table',
            url: '/admin/sitePage/',
            page : true,
			loading : true,
			height : 'full-115',
			limit : 10,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type:'radio'},
                {field: 'pageId', title: '#',width:60},
                {field: 'pageName', title: '页面标题'},
                {field: 'pageCode', title: '页面标识'},
                {field: 'orderNo',width:80,sort:true, title: '排序'},
                {field: 'state',width:100, title: '状态',templet: '#data-table-state'},
                {
                    field: 'createTime', sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center',width:320, toolbar: '#data-table-bar', title: '操作'}
            ]
            ]
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });

        function codeEdit(data) {
            layer.open({
                type: 1,
                title: '在线编辑',
                content: $('#runjs'),
                maxmin: true,
                area: ['90%', '90%'],
                success: function () {
                    $("#codePageId").val(data.pageId);
                    $('#runjs .CodeMirror').remove();
                    var mixedMode = {
                        name: "htmlmixed",
                        scriptTypes: [{
                            matches: /\/x-handlebars-template|\/x-mustache/i,
                            mode: null
                        },
                            {
                                matches: /(text|application)\/(x-)?vb(a|script)/i,
                                mode: "vbscript"
                            }]
                    };
                    window.editor = CodeMirror.fromTextArea(document.getElementById("code"), {
                        mode: mixedMode,
                        // 显示行号
                        lineNumbers: true,
                        // 括号匹配
                        matchBrackets: true,
                        theme: "darcula",
                        tabSize: 4,
                        //lineWrapping: true,
                        selectionPointer: true
                    });
                    //var ifr = document.getElementById("runjsDemo");

                    window.editor.setValue(data.contentHtml);
                    //$('#runjs textarea').val($('#runjs').html());
                }
            });
        }
        $('#code_save').click(function (e) {
            var pageId = $('#codePageId').val();
            var content = window.editor.getValue();
            if (B.isEmpty(pageId)) {
                B.error("pageId为空");
                return;
            }
            if (B.isEmpty(content)) {
                B.error("content为空");
                return;
            }
            $.post('/admin/sitePage/',{pageId:pageId,contentHtml:content},function (res) {
                if (res.success) {
                    B.info(res.msg);
                    tableIns.reload({},'data')
                }else{
                    B.info(res.msg);
                }
            })
        })
        // 工具条点击事件
        table.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            }else if (obj.event === 'copy') { //copy
                copyUrl(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }else if (obj.event === 'codeEdit') {
                codeEdit(data);
            }
        });
        layui.$('#LAY_demo_run').on('click', function() {
            var ifr = document.getElementById("runjsDemo");
            var code = window.editor.getValue();
            ifr.contentWindow.document.body.innerHTML = "";
            ifr.contentWindow.document.write($('#prefixHtml').html()+code);
            ifr.contentWindow.document.write('</div></div>');
            ifr.contentWindow.document.write('<script type="text/javascript" src="/assets/libs/layui/layui.js">'+'<'+'/script>');
            ifr.contentWindow.document.write('</body>');
            ifr.contentWindow.document.write('</html>');
        })
        // 复制
        var clipboard = new ClipboardJS('.btnCopy');
        clipboard.on('success', function (e) {
            e.clearSelection();
            layer.msg('地址已复制', {icon: 1});
        });
        clipboard.on('error', function (e) {
            layer.msg('复制失败，请手动复制', {icon: 2});
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edtSearch').val();
            tableIns.reload( {where: {pageName: keyword}},'data');
        });

        function doDelete(obj){
            top.layer.confirm('确定要删除吗？', function (i) {
                top.layer.close(i);
                B.del({
                    url:'/admin/sitePage/'+obj.data.pageId,
                    loading:true,
                    success:function(data){
                        if (data.success) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }
                });
            });
        }
        // 显示编辑弹窗
        var showEditModel = function (data) {
            admin.putTempData("t_page",data);
            admin.putTempData("formOk",false);
            var index = layer.open({
                type: 2,
                title: data ? '修改页面' : '添加页面',
                area: ['100%', '100%'],
                content: '/admin/sitePage/save',
                success : function(layero, index){
                    //var body = layui.layer.getChildFrame('body', index);
                    setTimeout(function(){
                        layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                },
                end:function () {
                    admin.getTempData('formOk') && tableIns.reload({},'data');  // 成功刷新表格
                }
            });
            layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layer.full(index);
            })

        };

        // 修改状态
        form.on('switch(data-table-state)', function (obj) {
            B.post({
                url: '/admin/sitePage/updateState',
                loading: true,
                data: {pageId: obj.elem.value, state: obj.elem.checked ? 0 : 1},
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });

        });

    });

</script>
</html>